//
//  selectedController.m
//  SendPresent
//
//  Created by hcl on 16/4/28.
//  Copyright © 2016年 hclong. All rights reserved.
//

#import "HCLSelectedController.h"
#import "HCLphotoCell.h"
#import "HCLGiftsModel.h"
@interface HCLSelectedController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
    UICollectionView *_collectionView;
    CGFloat _itemWH;
    CGFloat _margin;

}
//礼物数组
@property(nonatomic,copy)NSMutableArray * giftsArray;
//模型数组
@property(nonatomic,copy)NSMutableArray* modelArray;
//选中数组
@property(nonatomic,copy)NSMutableArray * selectedArray;
@end

@implementation HCLSelectedController




//强制横屏
-(instancetype)init{
    self = [super init];
    //就是这句
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationLandscapeRight;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"礼物栏";
    self.navigationItem.leftBarButtonItem.title = @"确定";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationController.navigationBar.alpha = 1.0;
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
 self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(sendGifts)];
    [self configCollectionView];
    
}
-(void)cancel{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"取消");
}
-(void)sendGifts{
    
    NSLog(@"确定");
    NSLog(@"%@",self.selectedArray);
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)viewWillAppear:(BOOL)animated{
    //隐藏状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    
    
}

-modelArray{
    if (!_modelArray) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
    
}
-(NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        NSMutableArray * array= [NSMutableArray array];
        _selectedArray = array;
    }
    return _selectedArray;
    
}
-(NSMutableArray *)giftsArray{
    if (!_giftsArray) {
        NSString * path = [[NSBundle mainBundle]pathForResource:@"Date" ofType:@"plist"];
        _giftsArray = [NSMutableArray arrayWithContentsOfFile:path];
    }
        return _giftsArray;
}

- (void)configCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat margin = 4;
    CGFloat itemWH = (self.view.frame.size.width- 2 * margin - 4) / 6 - margin;
    layout.itemSize = CGSizeMake(itemWH, itemWH+50);
    
    layout.minimumInteritemSpacing = margin;
    layout.minimumLineSpacing = margin;
    CGFloat top = margin + 44;
    
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,top-50, self.view.frame.size.width, self.view.frame.size.height  - top+50) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.alwaysBounceHorizontal = NO;
    _collectionView.contentInset = UIEdgeInsetsMake(4, 0, 0, 2);
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[HCLPhotoCell class] forCellWithReuseIdentifier:@"HCLSendPresentCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.giftsArray.count+1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //cell
    HCLPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HCLSendPresentCell" forIndexPath:indexPath];
    for (NSDictionary  * dir in self.giftsArray) {
        HCLGiftsModel * model = [HCLGiftsModel new];
        
        model.name  =    [dir objectForKey:@"petName"];
        model.PhotoName = [dir objectForKey:@"photoName"];
        
        [self.modelArray addObject:model];
    }
    

//    设置cell图片 内容；
    HCLGiftsModel * gift = self.modelArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:gift.PhotoName];
    cell.label.text = gift.name;


    if (indexPath.row == self.giftsArray.count) {
        cell.imageView.image = [UIImage imageNamed:@"icon_gwc.png"];
        cell.selectImageView.hidden = YES;
        cell.selectPhotoButton.hidden = YES;
        cell.label.text = nil;
    }
    cell.changed = ^(BOOL change){
        if (change) {
            NSLog(@"从数组移除");
            [self.selectedArray removeObject:gift.PhotoName];
        }else{
            NSLog(@"添加到数组");
            [self.selectedArray addObject:gift.PhotoName];
        }
        
    };
   
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.giftsArray.count) { [self pickPhotoButtonClick:nil];}else{
        NSLog(@"点击了第%ld个按钮",(long)indexPath.row+1);

    }
   }
-(void)pickPhotoButtonClick:(UIButton *)sender{
    NSLog(@"点击购物车");
}

@end
